﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors;
using MINERP.BLL.InfoConfig;
using MINERP.DAL;
using MINERP.MODEL.InfoConfig;
namespace MINERP.InfoConfig.Controller
{
    public  class FlowTemplateController
    {
        #region //private fields

        private ProductBLL _bll = null;
        private GridControl _grid = null;
        private GridView _view = null;
        private ComboBoxEdit _comboBox = null;
        private GroupControl _groupControl = null;
        private CheckEdit _checkEdit = null;
        private FlowTemplateEntity flowTemplateEntiry =null;
       
        private int _viewCount = 0;
        private List<FlowTemplateEntity> _flowTemplateEntityList = new List<FlowTemplateEntity>();

        #endregion


        #region //Public Properties

        internal GroupControl GroupControl
        {
            set
            {
                _groupControl = value;
            }
        }
        internal CheckEdit CheckEdit
        {
            set
            {
                _checkEdit = value;
            }
        }
        internal ComboBoxEdit ComoBox
        {
            set
            {
                _comboBox = value;
            }
        }
        internal GridControl Grid
        {
            set
            {
                _grid = value;
            }
        }

        internal GridView View
        {
            set
            {
                this._view = value;
            }
            get
            {
                return _view;
            }
        }

        internal List<FlowTemplateEntity> FlowTemplateEntityList
        {
            get
            {
                return _flowTemplateEntityList;
            }

        }

        #endregion


        #region //Public Methods

        public void ClearList()
        {
            _flowTemplateEntityList.Clear();
            _view.RefreshData();
        }
        public void LinkFlowTemplateView(int productId)
        {
            _bll = new ProductBLL();
            _flowTemplateEntityList.Clear();
            Tb_Product product = _bll.GetProductObj(productId);
            foreach (Tb_FlowTemplate obj in product.Tb_FlowTemplate)
            { 
                FlowTemplateEntity flowTemplate = new FlowTemplateEntity();
                flowTemplate.TechId = obj.tb_Tech.TechId;
                flowTemplate.FlowTemplateId = obj.FlowTemplateId;
                flowTemplate.TechName =obj.tb_Tech.TechName;
                flowTemplate.WorkOrder =obj.WorkOrder;
                flowTemplate.IsNecessery = obj.IsNecessery;
                _flowTemplateEntityList.Add(flowTemplate);
            }

            _grid.DataSource = _flowTemplateEntityList;
            _view.RefreshData();
        }
        public void AddTech(List< tb_Tech> list)
        {

            if (list.Count> 0)
            {
                foreach (var item in list)
                {
                    flowTemplateEntiry = new FlowTemplateEntity();
                    flowTemplateEntiry.TechId = item.TechId;
                    flowTemplateEntiry.TechName = item.TechName;
                    flowTemplateEntiry.WorkOrder = item.WokerOrder;
                    bool isIn = _flowTemplateEntityList.Contains(flowTemplateEntiry, new c());
                    if (!isIn)
                    {
                        _flowTemplateEntityList.Add(flowTemplateEntiry);
                        _flowTemplateEntityList.Sort(CompareOby);
                    }
                }
                //FlowTemplateEntity rsult=  _flowTemplateEntityList.Find(delegate(FlowTemplateEntity entiy)
                //{
                    
                //        if (entiy.TechId==flowTemplateEntiry.TechId)
                //        {
                //            return true;
                //        }
                //        else
                //        {
                //            return false;
                //        }
                //});
                //if (rsult==null)
                //{
                //    _flowTemplateEntityList.Add(flowTemplateEntiry);
                //}


                _grid.DataSource = _flowTemplateEntityList;
                _view.RefreshData();
            }
            else
            {
                MessageBox.Show("请选择要添加的节点", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        public class c : IEqualityComparer<FlowTemplateEntity>
        {
            public bool Equals(FlowTemplateEntity x, FlowTemplateEntity y)
            {
                if (x.TechId==y.TechId)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

            public int GetHashCode(FlowTemplateEntity a)
            {
                return 0;
            }
        }
        /// <summary>
        /// 获得焦点行的对象
        /// </summary>
        /// <returns></returns>
        public FlowTemplateEntity GetFocusFlowTemplateObj()
        {
            return (FlowTemplateEntity)_view.GetFocusedRow();
        }
        /// <summary>
        /// 删除选定节点
        /// </summary>
        /// <param name="obj"></param>
        public void DeleteFlowTemplate(FlowTemplateEntity obj )
        {
            _flowTemplateEntityList.Remove(obj);
            _view.RefreshData();
        }

        /// <summary>
        /// 把记录前移
        /// </summary>
        internal void PreRecord()
        {
            FlowTemplateEntity focusObj = (FlowTemplateEntity)this._view.GetFocusedRow();

            if (focusObj == null)
            {
                MessageBox.Show("没有选中任何行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            int index = _flowTemplateEntityList.IndexOf(focusObj);

            if (index < 1)
            {
                MessageBox.Show("已经是第一条记录，不能前移", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            FlowTemplateEntity preObj = _flowTemplateEntityList.ElementAt(index - 1);

            //改变工序的值
            int tempOrder = focusObj.WorkOrder;
            focusObj.WorkOrder = preObj.WorkOrder;
            preObj.WorkOrder = tempOrder;

            //交换位置
            _flowTemplateEntityList.Sort(CompareOby);

            this._view.RefreshData();
            this._view.FocusedRowHandle = index - 1;

            //_bll.Update();
        }

        /// <summary>
        /// 把记录后移
        /// </summary>
        internal void NextRecord()
        {
            _viewCount = this._view.RowCount;

            FlowTemplateEntity focusObj = (FlowTemplateEntity)this._view.GetFocusedRow();

            if (focusObj == null)
            {
                MessageBox.Show("没有选中任何行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            int index = _flowTemplateEntityList.IndexOf(focusObj);


            if (index == _viewCount - 1)
            {
                MessageBox.Show("已经是最后一条记录，不能后移", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            FlowTemplateEntity nextObj = _flowTemplateEntityList.ElementAt(index + 1);

            //改变工序的值
            int tempOrder = focusObj.WorkOrder;
            focusObj.WorkOrder = nextObj.WorkOrder;
            nextObj.WorkOrder = tempOrder;

            //交换位置
            _flowTemplateEntityList.Sort(CompareOby);

            this._view.RefreshData();
            this._view.FocusedRowHandle = index + 1;

            //_bll.Update();
        }

        #endregion

        #region //helper Methods
        private int CompareOby(FlowTemplateEntity objA, FlowTemplateEntity objB)
        {
            return objA.WorkOrder - objB.WorkOrder;
        }

        #endregion
    }
}
